<?php
/**
 * @msg 主要获取首页视频列表（手动/自动结合）
 * @file videolistcontroller.php
 * @author YueBin
 * @date 2016-06-14 11:20:57
 * @brief 
 */
namespace haibao\cms\business\control\pc\index;

use haibao\cms\model\filter\Video as VideoFilter;
use haibao\cms\model\filter\VideoCategory as VideoCategoryFilter;
use haibao\frame\data\query\Condition;

class VideoListController extends IndexController{

    public function __construct($updateTime = null,$controlId,$isPublish){
        parent::__construct($updateTime,$controlId,$isPublish);
    }
    
    /**
     * 显示模版内容（模版动态数据的绑定和模版输出）
     * @param : String $content
     * @param : Array $controlData;
     */
    public function displayTemplateHtml(){
    
        $controlData = $this->getParameter(self::$CONTROL_DATA);
        $content = $this->getParameter(self::$CONTROL_CONTENT);
        $path = $this->generateHtmlPath(__CLASS__);
        $url = $title = $href = '';
        $manualVideoArr = $manualVideoIdArr = $lastVideoList = array();
        if($controlData['auto']){
            foreach($controlData['auto'] as $auto){
                $paramVal = json_decode($auto['ParameterValue'],true);
                $videoInfo = explode(',',$paramVal['Id']);
                if($videoInfo[1] == \haibao\cms\business\control\pc\action\ArticleAction::INFO_TYPE_VIDEO){
                	$paramVal['Id'] = $videoInfo[0];
                	$manualVideoArr[$videoInfo[0]] = $paramVal;
                	array_push($manualVideoIdArr,$videoInfo[0]);
                }
            }
            if($manualVideoIdArr){
                $queryVideoList = $this->getVideoList($manualVideoIdArr,false);
                if($queryVideoList){
                    foreach($manualVideoArr as $key=>$val){
                        if(isset($queryVideoList[$val['Id']])){
                            if(empty($manualVideoArr[$key]['HrefUrl'])){
                                $manualVideoArr[$key]['HrefUrl'] = $queryVideoList[$val['Id']]['HrefUrl'];
                            }
                            if(empty($manualVideoArr[$key]['Title'])){
                                $manualVideoArr[$key]['Title'] = $queryVideoList[$val['Id']]['Title'];
                            }
                            if(empty($manualVideoArr[$key]['Description'])){
                                $manualVideoArr[$key]['Description'] = $queryVideoList[$val['Id']]['Description'];
                            }
                            if(empty($manualVideoArr[$key]['PicUrl'])){
                                $manualVideoArr[$key]['PicUrl'] = $queryVideoList[$val['Id']]['PicUrl'];
                            }
                            $manualVideoArr[$key]['CategoryId'] = $queryVideoList[$val['Id']]['CategoryId'];
                            $manualVideoArr[$key]['CategoryName'] = $queryVideoList[$val['Id']]['CategoryName'];
                        }
            
                    }
                }
            }
        }
        $manualCnt = count($manualVideoArr);
    	if($manualCnt < 7){
    		$lastVideoList = $this->getVideoList($manualVideoIdArr,true);
    	}else{
    	    $manualVideoArr = array_slice($manualVideoArr,0,7,true);
    	}
    	$finalArr = array_merge($manualVideoArr,$lastVideoList);
        $videoLeft = $videoRight = array();
        $videoLeft = array_slice($finalArr,0,1);
        $videoRight = array_slice($finalArr,1,6);
        $this->assign('videoLeft', $videoLeft);
        $this->assign('videoRight', $videoRight);
        $this->assign('exclusiveUrl', \haibao\classlibrary\cms\Config::getConfig(\haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_HOST_VIDEO));
        $isNewHtml = $this->generateHtml($path,$content);
        
        $content = $this->getHtml($path);
        // 解析内容 更换链接
        $content = $this->replaceCDNLink($content);
        return $content;
    }

    
    private function getVideoList($videoId,$isRemove = false){
    	$videoFilter = new VideoFilter();
    	$videoFilter->select(array(
    			VideoFilter::CONDITION_FIELD_NAME_ID,
    			VideoFilter::CONDITION_FIELD_NAME_CATEGORYID,
    			VideoFilter::CONDITION_FIELD_NAME_TITLE,
    			VideoFilter::CONDITION_FIELD_NAME_PUBLISHDATE,
    			VideoFilter::CONDOTION_FIELD_NAME_DESCRIPTION,
    			VideoFilter::CONDITION_FIELD_NAME_PROPICURL,
    	        VideoFilter::CONDITION_FIELD_NAME_FEATUREID
    	));
    	$videoFilter->order(VideoFilter::CONDITION_FIELD_NAME_PUBLISHDATE,false);
    	$videoFilter->where(VideoFilter::CONDITION_FIELD_NAME_STATUS, Condition::CONDITION_EQUAL,\haibao\cms\model\data\Video::STATUS_ENABLE);
    	$videoFilter->where(VideoFilter::CONDITION_FIELD_NAME_PUBLISHED, Condition::CONDITION_EQUAL,\haibao\cms\model\data\Video::STATUS_ENABLE);
    	if($isRemove){
    		$videoFilter->limit(7-count($videoId));
    		if($videoId){
    			$videoFilter->where(VideoFilter::CONDITION_FIELD_NAME_ID,Condition::CONDITION_NOTIN,$videoId);
    		}
    	}else{
    		$videoFilter->where(VideoFilter::CONDITION_FIELD_NAME_ID,Condition::CONDITION_IN,$videoId);
    	}
    	$videoCategoryFilter = new VideoCategoryFilter();
    	$videoCategoryFilter->select(array(
    			VideoCategoryFilter::CONDITION_FIELD_NAME_ID,
    			VideoCategoryFilter::CONDITION_FIELD_NAME_CATEGORYNAME
    	));
    	$videoCategoryFilter->where(VideoCategoryFilter::CONDITION_FIELD_NAME_STATUS, Condition::CONDITION_EQUAL, \haibao\cms\model\data\VideoCategory::STATUS_ENABLE);
    	$videoFilter->leftJoin($videoCategoryFilter,null,VideoFilter::CONDITION_FIELD_NAME_CATEGORYID,VideoCategoryFilter::CONDITION_FIELD_NAME_ID,VideoFilter::CONDITION_FIELD_NAME_CATEGORY,false);
    	
    	$data = new \haibao\cms\data\Video();
    	$result = $data->getAll($videoFilter);
    	$videoArr = $featureIdArr = $featureArr = array();
    	if($result){
    	    foreach($result as $key=>$val){
    	        $vid['Id'] = $val->Id;
    	        $vid['CategoryId'] = $val->CategoryId;
    	        $vid['Title'] = $val->Title;
    	        $vid['CategoryName'] = $val->Category->CategoryName;
    	        $vid['PicUrl'] = $val->ProPicUrl;
    	        $vid['FeatureId'] = $val->FeatureId;
    	        $vid['Description'] = $val->Description;
    	        $vid['HrefUrl'] = \haibao\classlibrary\cms\Config::getConfig(\haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_HOST_VIDEO).'/play/'.$val->Id.'.htm';
    	        $videoArr[$val->Id] = $vid;
    	        $featureIdArr[] = $val->FeatureId;
    	    }
    	}
    	if($featureIdArr){
    		$featureList = \haibao\cms\business\FeaturePublish::getFeatureByIds($featureIdArr);
	    	if($featureList){
	    	    foreach($featureList as $key=>$val){
	    	        $featureArr[$val->Id] = $val->Url;
	    	    }
	    	}
	    	if($videoArr){
	    	    foreach($videoArr as $key=>$val){
	    	        if(isset($featureArr[$val['FeatureId']])){
	    	            $videoArr[$key]['HrefUrl'] = $featureArr[$val['FeatureId']];
	    	        }
	    	    }
	    	}
    	}
    	return $videoArr;
    }
} 